package com.supermarket.service.impl;

import com.alibaba.excel.EasyExcel;
import com.supermarket.entity.Orders;
import com.supermarket.listener.OrdersListener;
import com.supermarket.mapper.OrdersMapper;
import com.supermarket.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired//自动装配给ordersSercice
    private OrdersMapper ordersMapper;

    @Override
    public List<Orders> findAll() {
        return ordersMapper.findAll();
    }

    @Override
    public void add(Orders orders) {
        ordersMapper.add(orders);
    }

    @Override
    public void deleteById(String orderID) {
        ordersMapper.deleteById(orderID);
    }

    @Override
    public void updateOrders(Orders orders) {
        ordersMapper.updateOrders(orders);
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        ordersMapper.deleteByIds(ids);
    }

    @Override
    public List<Orders> findByIds(Integer[] ids) {return ordersMapper.findByIds(ids);}

    @Override
    public void save(MultipartFile file) {
        // 有个很重要的点 DemoDataListener 不能被spring管理，要每次读取excel都要new,然后里面用到spring可以构造方法传进去
        // 写法3：
        //fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";


        InputStream inputStream = null;

        //通过流来读取上传的文件
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        EasyExcel.read(inputStream, Orders.class, new OrdersListener(ordersMapper)).sheet().doRead();

    }
}
